<?php

class Default_AuthController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$form = new Application_Form_Login();
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				if ($this->_process($form->getValues())) {
					// We're authenticated! Redirect to the home page
					$this->_helper->redirector('index','auth');
				}
			}
		}
		$this->view->form = $form;
		//print_r($_COOKIE);

	}

	protected function _process($values) {
		 
		$adapter = $this->_getAuthAdapter();
		$adapter->setIdentity($values['email']);
		$adapter->setCredential($values['password']);
		$adapter->setCredentialTreatment('SHA1(CONCAT(?,hashcode))');

		$emailModel = new Application_Model_Dao_User();

		$hashcode = $emailModel->getHashcode($values['email']);


		$pass = SHA1($values['password'].$hashcode);
		$auth = Zend_Auth::getInstance();
		$result = $auth->authenticate($adapter);
		if ($result->isValid()) {
			if($values['remember']==1){

				$this->setCookie($values['email'],$pass);
			}

			$user = $adapter->getResultRowObject();
			$auth->getStorage()->write($user);
			return true;
		}
		return false;
	}

	protected function _getAuthAdapter()
	{
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'users','email','password');


		return $authAdapter;
	}

	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		// print_r($_COOKIE);
		$this->unsetCookie();
		// print_r($_COOKIE);

		$this->_helper->redirector('index','auth'); // back to login page
	}

	private function setCookie($login, $password)
	{
		setcookie('log',$login, time()+3600*2, '/');
		setcookie('pas',$password, time()+ 3600*2, '/');
	}

	private function unsetCookie()
	{
		setcookie('log','', time()-3600, '/');
		setcookie('pas','', time()-3600, '/');
	}

	public function resetowanieAction()

	{
		$form = new Application_Form_Email();
		$this->view->form = $form;
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$emailModel = new Application_Model_DbTable_Uzytkownicy();
				$pass = $this->resetPass();
				$email = $form->getValue('email');
				$hashcode = $emailModel->getHashcode($email);
				$newPass = SHA1($pass.$hashset);
				//$sendMail = $this->sendMail($emial, $newPass);
				//if($sendMail){
				 
					$emailModel->updatePassword($email, $newPass);
					 
					$this->_helper->redirector('index','auth');
					//}else{
					// echo "Nie wyslano emaila";
					//}
					//}
					 
			}


		}
	}
	private function resetPass($length = 11){
		$newPass = md5(time());
		$newPass = substr($newPass,0,$length);
		return($newPass);
	}
	private function sendMail($email, $password){
		$mail = new Zend_Mail();
		$mail->setBodyText('Login: '.$email.'
Haslo: '.$password);
		$mail->setFrom('somebody@example.com', 'lukasz kokosza');
		$mail->addTo($email);
		$mail->setSubject('Nowe Haslo');
		//sprawdzic czy zwroci co zwroci?:)
		return $mail->send();
	}
	private function hashPass($pass, $hashcode){
		return SHA1($pass.$hashcode);
	}
	private function hashHashcode($hashcode){
		return SHA1($hashcode);
	}


}







